import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:package_info/package_info.dart';

import 'package:search_people/utils/PrivacyView.dart';
import 'package:search_people/utils/storage.dart';

import '../../utils/util.dart';
import 'index.dart';

class SplashLogic extends GetxController {
  SplashLogic();

  final state = SplashState();

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() async {
    super.onInit();
    PackageInfo.fromPlatform().then((value) {
      state.appName = value.appName;
      update();
    });
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
    state.timer = Timer.periodic(const Duration(seconds: 1), (_) {
      if (state.seconds == 0) {
        toMain();
      }
      state.seconds -= 1;

      if (state.seconds >= 0) {
        update();
      }
    });
  }

  void toMain() async {
    state.timer?.cancel();
    await SpUtil().init();
    var isFirstLaunch = SpUtil().getBool(StorageKeys.firstLaunch);
    if (isFirstLaunch == false) {
      Get.offAndToNamed(RouterPath.bottom_nav);
    } else {
      showPrivacyDialog();
    }
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
    state.timer = null;
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }

  showPrivacyDialog() {
    showDialog(
        context: Get.context!,
        barrierDismissible: false,
        barrierColor: Color(0x50000000),
        builder: (context) {
          return Container(
            // height: 500.w,
            // width: 200.h,
            margin: EdgeInsets.fromLTRB(40, 60, 40, 60),
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(10))),

            child: Column(
              children: [
                Container(
                  height: 45,
                  alignment: Alignment.center,
                  child: Text(
                    '用户隐私政策',
                    style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                  ),
                ),
                Divider(
                  height: 1,
                ),
                Expanded(
                    child: Padding(
                  padding: const EdgeInsets.fromLTRB(8.0, 1, 8, 1),
                  child: SingleChildScrollView(
                    child: PrivacyView(
                      data: state.data,
                      keys: ['《用户协议》', '《隐私政策》'],
                      keyStyle: TextStyle(color: Colors.red),
                      onTapCallback: (String key) {
                        if (key == '《用户协议》') {
                          // Navigator.of(context)
                          //     .push(MaterialPageRoute(builder: (context) {
                          //   return WebViewPage(
                          //       title: key, url: 'https://flutter.dev');
                          // }));
                        } else if (key == '《隐私政策》') {
                          // Navigator.of(context)
                          //     .push(MaterialPageRoute(builder: (context) {
                          //   return WebViewPage(
                          //       title: key, url: 'https://www.baidu.com');
                          // }));
                        }
                      },
                    ),
                  ),
                )),
                Divider(
                  height: 1,
                ),
                Container(
                  height: 45,
                  child: Row(
                    children: [
                      Expanded(
                          child: GestureDetector(
                        child: Container(
                            alignment: Alignment.center, child: Text('不同意')),
                        onTap: () {
                          exit(0);
                        },
                      )),
                      VerticalDivider(
                        width: 1,
                      ),
                      Expanded(
                          child: GestureDetector(
                        child: Container(
                          alignment: Alignment.center,
                          child: Text('同意'),
                          decoration: BoxDecoration(
                              color: Theme.of(context).primaryColor,
                              borderRadius: BorderRadius.only(
                                  bottomRight: Radius.circular(10))),
                        ),
                        onTap: () {
                          Get.offAndToNamed(RouterPath.bottom_nav);
                          SpUtil().setBool(StorageKeys.firstLaunch, false);
                        },
                      )),
                    ],
                  ),
                ),
              ],
            ),
          );
        });
  }
}
